﻿using CTM.Web.Models.Input;
using CTM.Web.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace CTM.Web.Models.View.Conversation
{
    public class ConversationViewModel
    {
        #region Properties

        [Key]
        public Guid Id { get; set; }

        [Required(ErrorMessage = "*")]
        [StringLength(100)]
        public string Title { get; set; }

        [Required(ErrorMessage = "*")]
        //[Display(Name = "Tên role")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required(ErrorMessage = "*")]
        public PostType Type { get; set; }

        [Required(ErrorMessage = "*")]
        public Emoticon Emoticon { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime CreatedOn { get; set; }

        [StringLength(128)]
        public string CreatedBy { get; set; }

        public DateTime? ModifiedOn { get; set; }

        [StringLength(128)]
        public string ModifiedBy { get; set; }

        [StringLength(128)]
        [Display(Name = "Manager")]
        [Required(ErrorMessage = "*")]
        public string ManagerId { get; set; }

        #endregion

        #region Relationship
        public virtual Category Category { get; set; }
        public virtual ICollection<Input.Comment> Comments { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }
        public virtual ApplicationUser Manager { get; set; }
        public virtual ApplicationUser Employee { get; set; }
        #endregion

        public ConversationViewModel()
        {
            Type = PostType.Individual;
            Emoticon = Emoticon.Neutral;
            IsDeleted = false;
            CreatedOn = DateTime.UtcNow;
            Comments = new HashSet<Input.Comment>();
            Tags = new HashSet<Tag>();
        }

        public ConversationViewModel(Input.Post conversation)
        {
            this.Id = conversation.Id;
            this.Title = conversation.Title;
            this.Description = conversation.Description;
            this.Type = conversation.Type.Value;
            this.Emoticon = conversation.Emoticon.Value;
            this.IsDeleted = conversation.IsDeleted;            
            this.CreatedOn = ApplicationHelpers.ConvertToLocalTime(conversation.CreatedOn);
            this.CreatedBy = conversation.CreatedBy;
            this.ModifiedOn = conversation.ModifiedOn.HasValue ? ApplicationHelpers.ConvertToLocalTime(conversation.ModifiedOn.Value) : conversation.ModifiedOn;
            this.ModifiedBy = conversation.ModifiedBy;
            this.Category = conversation.Category;
            this.Comments = conversation.Comments;
            this.Tags = conversation.Tags;
            this.Manager = conversation.Manager;
            this.ManagerId = conversation.Manager.Id;
            this.Employee = conversation.Employee;            
        }
    }
}
